package com.team.controller;


import com.alibaba.excel.EasyExcel;
import com.team.easyexcel.DiseaseExcel;
import com.team.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * excel控制器
 *
 * @author 陈灰灰
 * @date 2022/10/24
 */
@RestController
public class ExcelController {
    @Autowired
    private ReservationService reservationService;


    @RequestMapping("download")
    public void download(HttpServletResponse response ,int reservationId ) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("病例报告", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        DiseaseExcel diseaseExcel = reservationService.queryByReservationIdAndDown(reservationId);
        List<DiseaseExcel> data = new ArrayList<>();
        data.add(diseaseExcel);
        System.out.println(data);
        EasyExcel.write(response.getOutputStream(), DiseaseExcel.class).sheet("病例报告").doWrite(data);
    }

}
